import * as types from './mutation-types'
import {isType} from '@/utils/monitor'
import Vue from 'vue'
// import {getCache} from '@/utils/m7Utils.js'
// 服务号
// messageSession 留言数
// waitClaim 待领取会话数
// dealingSession 正在咨询人数
//
// 技能组
// undealSession 未接入会话数
// messageSession 留言数
// queueGiveupCount 排队放弃数
export default {
  [types.INIT_AGENT_AND_QUEUE] (state, {res, mailQueues, onlines}) {
    for (let i = 0; i < res.onlineList.length; i++) {
      if (!state.table.agentMonitor[res.onlineList[i]._id]) {
        // 返回数据的_id所对应的坐席，在agent里面找不到，跳过该条
        continue
      }
      if ('emailStatus' in res.onlineList[i]) {
        if (res.onlineList[i].emailStatus === 'close') {
          Vue.set(state.table.agentMonitor[res.onlineList[i]._id], 'mailAutoClaim', false)
        } else {
          Vue.set(state.table.agentMonitor[res.onlineList[i]._id], 'mailAutoClaim', true)
        }
      } else {
        Vue.set(state.table.agentMonitor[res.onlineList[i]._id], 'mailAutoClaim', false)
      }
      let user = res.onlineList[i].user || {}
      Vue.set(state.table.agentMonitor[res.onlineList[i]._id], 'pcState', user.pcState)
      Vue.set(state.table.agentMonitor[res.onlineList[i]._id], 'mobileState', user.mobileState)
      Vue.set(state.table.agentMonitor[res.onlineList[i]._id], 'pcAutoClaim', user.pcAutoClaim)
      Vue.set(state.table.agentMonitor[res.onlineList[i]._id], 'pcToggleNew', user.pcToggleNew)
      Vue.set(state.table.agentMonitor[res.onlineList[i]._id], 'mobileAutoClaim', user.mobileAutoClaim)
      // app 或pc领取开
      Vue.set(state.table.agentMonitor[res.onlineList[i]._id], 'autoClaim', user.pcAutoClaim || user.mobileAutoClaim)
      Vue.set(state.table.agentMonitor[res.onlineList[i]._id], 'pcClaimTime', user.pcClaimTime)
      Vue.set(state.table.agentMonitor[res.onlineList[i]._id], 'mobileClaimTime', user.mobileClaimTime)
      Vue.set(state.table.agentMonitor[res.onlineList[i]._id], 'imDealCount', user.imDealCount)
      // _im_monitor_onPeerChanged(state.table.agentMonitor[res.onlineList[i]._id])
      // 监控模块自定义状态时长处理
      Vue.set(state.table.agentMonitor[res.onlineList[i]._id], 'pcCloseClaimTime', user.pcCloseClaimTime)
      Vue.set(state.table.agentMonitor[res.onlineList[i]._id], 'pcClaimState', user.pcClaimState)
      // Vue.set(state.table.agentMonitor[res.onlineList[i]._id], 'mobileCloseClaimTime', user.mobileCloseClaimTime)
    }
    for (let i = 0; i < res.dealList.length; i++) {
      if (state.table.agentMonitor[res.dealList[i]._id]) {
        Vue.set(state.table.agentMonitor[res.dealList[i]._id], 'dealing', res.dealList[i].count)
      }
    }
    // queue table init need agentData so in there init queues.
    let imQueues = mailQueues
    let skill = {}
    for (let i = 0; i < imQueues.length; i++) {
      // imQueues[i].manualSession = 0
      // imQueues[i].waitClaim = 0
      // imQueues[i].undealSession = 0
      // imQueues[i].messageSession = 0
      Vue.set(imQueues[i], 'manualSession', 0)
      Vue.set(imQueues[i], 'waitClaim', 0)
      Vue.set(imQueues[i], 'undealSession', 0)
      Vue.set(imQueues[i], 'messageSession', 0)
      Vue.set(imQueues[i], 'queueGiveupCount', 0)
      skill[imQueues[i].Exten] = imQueues[i]
      // Vue.set(state.table.MultiChannelSkill, imQueues[i].Exten, imQueues[i])
    }
    Vue.set(state.table, 'MultiChannelSkill', skill)
    for (let i = 0; i < res.queueAccessList.length; i++) {
      if (state.table.MultiChannelSkill[res.queueAccessList[i]._id]) {
        Vue.set(state.table.MultiChannelSkill[res.queueAccessList[i]._id], 'manualSession', res.queueAccessList[i].accessCount)
      }
    }
    for (let i = 0; i < res.queueUndealList.length; i++) {
      if (state.table.MultiChannelSkill[res.queueUndealList[i]._id]) {
        Vue.set(state.table.MultiChannelSkill[res.queueUndealList[i]._id], 'waitClaim', res.queueUndealList[i].undealCount)
      }
    }
    let messageSessionList = res.messageSessionList || []
    for (let i = 0; i < messageSessionList.length; i++) {
      if (state.table.MultiChannelSkill[res.messageSessionList[i]._id]) {
        Vue.set(state.table.MultiChannelSkill[res.messageSessionList[i]._id], 'messageSession', res.messageSessionList[i].messageSession)
      }
    }
    let undealSessionList = res.undealSessionList || []
    for (let i = 0; i < undealSessionList.length; i++) {
      if (state.table.MultiChannelSkill[res.undealSessionList[i]._id]) {
        Vue.set(state.table.MultiChannelSkill[res.undealSessionList[i]._id], 'undealSession', res.undealSessionList[i].undealSession)
      }
    }
    let queueGiveupList = res.queueGiveupList || []
    for (let i = 0; i < queueGiveupList.length; i++) {
      if (state.table.MultiChannelSkill[res.queueGiveupList[i]._id]) {
        Vue.set(state.table.MultiChannelSkill[res.queueGiveupList[i]._id], 'queueGiveupCount', res.queueGiveupList[i].queueGiveupCount)
      }
    }
    for (let i in state.table.agentMonitor) {
      let item = state.table.agentMonitor[i]
      Vue.set(state.table.agentMonitor[i], 'agentState', peersState(item))
      peersStateChange(state, state.table.agentMonitor[i], 'plus')
    }
    for (let i in state.table.MultiChannelSkill) {
      imMonitorInitQueueAgentsNew(state.table.MultiChannelSkill[i], onlines, state)
    }
    skillSummary(state)
  },
  [types.INIT_MONITOR_AGENTS] (state, {agents}) {
    let agentMonitor = {}
    for (let i = 0; i < agents.length; i++) {
      if (agents[i].status === 'disable') {
        continue
      }
      let agent = {_id: agents[i]._id, autoOrderInitStatus: agents[i].autoOrderInitStatus || 'busy', exten: agents[i].exten, displayName: agents[i].displayName}
      agent.claim = 0
      agent.invitsession = 0
      agent.dealing = 0
      agent.autoClaim = false
      agent.mailAutoClaim = false // 邮件开关
      agent.pcState = false
      agent.sessionRollInCount = 0 // 转入会话数
      agent.sessionRollOutCount = 0 // 转出会话数
      agent.sessionCompleteCount = 0 // 完成会话数
      agent.agentInviteCount = 0 // 坐席会话邀请成功数
      agent.mobileState = 0
      agent.pcAutoClaim = false
      agent.pcToggleNew = ''
      agent.mobileAutoClaim = false
      agent.mobileClaimTime = 0
      agent.pcClaimTime = 0
      agent.pcClaimState = 0          // 座席领取开关状态
      agent.pcCloseClaimTime = 0      // 自定义状态时长
      // agent.mobileCloseClaimTime = 0  // 移动端自定义状态时长
      agent.imDealCount = ''
      agentMonitor[agents[i]._id] = agent
      // Vue.set(state.table.agentMonitor, agents[i]._id, agent)
    }
    // state.table.agentMonitor = agentMonitor
    Vue.set(state.table, 'agentMonitor', agentMonitor)
  },
  [types.PHONE_HEARTBEAT] (state, {pbx, data}) {
    Vue.set(state.table.phone_heartBeat, pbx, data)
  },
  [types.QUEUE_WARNING] (state, {data}) {
    Vue.set(state, 'queueWarning', data)
  },
  [types.UPDATE_CURRENT_QUEUE_SERVICENO] (state, {data}) {
    Vue.set(state, 'updateCurrentQueueServiceNo', data)
  },
  [types.INIT_MONITOR_IM_CHANNEL] (state, {config}) {
    let multiChannel = {}
    for (let i = 0; i < config.length; i++) {
      config[i].newSession = 0 // 今日会话数
      config[i].accessSession = 0 // 今日已处理会话数
      config[i].closeSession = 0 // 今日完成会话数
      config[i].robotSession = 0
      config[i].manualSession = 0
      config[i].robotToManualSession = 0
      if (config[i].nick_name) {
        // 如果是微信,特殊处理,因为字段名不同
        config[i].accessId = config[i]._id
        config[i].name = config[i].nick_name
        config[i].platform = 'weixin'
        config[i].ToPeer = config[i].to_peer
      }
      let data = {_id: config[i].accessId || config[i]._id,
        name: config[i].name,
        newSession: config[i].newSession,
        accessSession: config[i].accessSession,
        closeSession: config[i].closeSession,
        messageSession: config[i].messageSession || 0,
        waitClaim: config[i].waitClaim || 0,
        dealingSession: config[i].dealingSession || 0
      }
       // messageSession 留言数
      // waitClaim 待领取会话数
      // dealingSession 正在咨询人数
      multiChannel[data._id] = data
      // Vue.set(state.table.MultiChannel, data._id, data)
    }
    // state.table.MultiChannel = multiChannel
    Vue.set(state.table, 'MultiChannel', multiChannel)
  },
  [types.INIT_MONITOR_ACCESSNO] (state, {res}) {
    for (let i = 0; i < res.agentList.length; i++) {
      Vue.set(state.table.agentMonitor[res.agentList[i]._id], 'claim', res.agentList[i].count)
      Vue.set(state.table.agentMonitor[res.agentList[i]._id], 'invitsession', res.agentList[i].invitsessionCount)
      Vue.set(state.table.agentMonitor[res.agentList[i]._id], 'sessionRollInCount', res.agentList[i].sessionRollInCount)
      Vue.set(state.table.agentMonitor[res.agentList[i]._id], 'sessionRollOutCount', res.agentList[i].sessionRollOutCount)
      Vue.set(state.table.agentMonitor[res.agentList[i]._id], 'sessionCompleteCount', res.agentList[i].sessionCompleteCount)
      Vue.set(state.table.agentMonitor[res.agentList[i]._id], 'agentInviteCount', res.agentList[i].agentInviteCount)
    }
    for (let i = 0; i < res.accessList.length; i++) {
      let accessId = res.accessList[i].accessId
      if (state.table.MultiChannel[accessId]) {
        if ('totalSessionCount' in res.accessList[i]) {
          Vue.set(state.table.MultiChannel[accessId], 'newSession', res.accessList[i].totalSessionCount)
        }
        if ('accessSessionCount' in res.accessList[i]) {
          Vue.set(state.table.MultiChannel[accessId], 'accessSession', res.accessList[i].accessSessionCount)
        }
        if ('finishSessionCount' in res.accessList[i]) {
          Vue.set(state.table.MultiChannel[accessId], 'closeSession', res.accessList[i].finishSessionCount)
        }
        if ('robotSessionCount' in res.accessList[i]) {
          Vue.set(state.table.MultiChannel[accessId], 'robotSession', res.accessList[i].robotSessionCount)
        }
        if ('manualSessionCount' in res.accessList[i]) {
          Vue.set(state.table.MultiChannel[accessId], 'manualSession', res.accessList[i].manualSessionCount)
        }
        if ('robotToManualSessionCount' in res.accessList[i]) {
          Vue.set(state.table.MultiChannel[accessId], 'robotToManualSession', res.accessList[i].robotToManualSessionCount)
        }
        // messageSession 留言数
        // waitClaim 待领取会话数
        // dealingSession 正在咨询人数
        if ('messageSession' in res.accessList[i]) {
          Vue.set(state.table.MultiChannel[accessId], 'messageSession', res.accessList[i].messageSession)
        }
        if ('waitClaim' in res.accessList[i]) {
          Vue.set(state.table.MultiChannel[accessId], 'waitClaim', res.accessList[i].waitClaim)
        }
        if ('dealingSession' in res.accessList[i]) {
          Vue.set(state.table.MultiChannel[accessId], 'dealingSession', res.accessList[i].dealingSession)
        }
      }
    }
    accessSummary(state)
  },
  [types.EVENT_MONITOR_IM_CHANNEL] (state, {event, curAccess}) {
    if (!state.table.MultiChannel[event.accessId]) {
      if (curAccess) {
        if (curAccess.nick_name) {
          curAccess.accessId = curAccess._id
          curAccess.name = curAccess.nick_name
          curAccess.platform = 'weixin'
          curAccess.ToPeer = curAccess.to_peer
          Vue.set(state.table.MultiChannel, curAccess._id, curAccess)
        } else {
          Vue.set(state.table.MultiChannel, curAccess.accessId, curAccess)
        }
      }
    }
    if (!state.table.MultiChannel[event.accessId]) {
      let accessNo = {
        accessId: event.accessId,
        newSession: event.newSession,
        accessSession: event.accessSession,
        closeSession: event.closeSession,
        robotSession: event.robotSession,
        manualSession: event.manualSession,
        robotToManualSession: event.robotToManualSession,
        messageSession: event.messageSession,
        waitClaim: event.waitClaim,
        dealingSession: event.dealingSession
      }
      Vue.set(state.table.MultiChannel, event.accessId, accessNo)
    } else {
      Vue.set(state.table.MultiChannel[event.accessId], 'newSession', event.newSession)
      Vue.set(state.table.MultiChannel[event.accessId], 'closeSession', event.closeSession)
      Vue.set(state.table.MultiChannel[event.accessId], 'robotSession', event.robotSession)
      Vue.set(state.table.MultiChannel[event.accessId], 'manualSession', event.manualSession)
      Vue.set(state.table.MultiChannel[event.accessId], 'accessSession', event.accessSession)
      Vue.set(state.table.MultiChannel[event.accessId], 'robotToManualSession', event.robotToManualSession)
      Vue.set(state.table.MultiChannel[event.accessId], 'messageSession', event.messageSession || 0)
      Vue.set(state.table.MultiChannel[event.accessId], 'waitClaim', event.waitClaim || 0)
      Vue.set(state.table.MultiChannel[event.accessId], 'dealingSession', event.dealingSession || 0)
    }
    accessSummary(state)
  },
  [types.EVENT_MONITOR_IM_AGENT] (state, {event}) {
    let agent
    if (!state.table.agentMonitor[event._id]) {
      agent = {
        _id: event._id,
        claim: event.claim, // 接入会话数
        invitsession: event.invitsession, // 会话邀请数
        dealing: event.dealing, // 当前会话数
        autoClaim: event.autoClaim,
        mailAutoClaim: event.mailAutoClaim, // 邮件开关
        sessionRollInCount: event.sessionRollInCount, // 转入会话数
        sessionRollOutCount: event.sessionRollOutCount, // 转出会话数
        sessionCompleteCount: event.sessionCompleteCount, // 完成会话数
        agentInviteCount: event.agentInviteCount, // 坐席会话邀请成功数
        pcState: event.pcState,
        mobileState: event.mobileState,
        pcAutoClaim: event.pcAutoClaim,
        pcToggleNew: event.pcToggleNew,
        mobileAutoClaim: event.mobileAutoClaim,
        mobileClaimTime: event.mobileClaimTime,
        pcClaimTime: event.pcClaimTime,
        pcClaimState: event.pcClaimState,          // 座席领取开关状态
        pcCloseClaimTime: event.pcCloseClaimTime,      // 自定义状态时长
        // mobileCloseClaimTime: event.mobileCloseClaimTime,  // 移动端自定义状态时长
        imDealCount: event.imDealCount
      }
      Vue.set(state.table.agentMonitor, event._id, agent)
      peersStateChange(state, state.table.agentMonitor[event._id], 'plus')
    } else {
      peersStateChange(state, state.table.agentMonitor[event._id], 'minus')
      Vue.set(state.table.agentMonitor[event._id], 'claim', event.claim)
      Vue.set(state.table.agentMonitor[event._id], 'invitsession', event.invitsession)
      Vue.set(state.table.agentMonitor[event._id], 'dealing', event.dealing)
      Vue.set(state.table.agentMonitor[event._id], 'autoClaim', event.autoClaim)
      Vue.set(state.table.agentMonitor[event._id], 'sessionRollInCount', event.sessionRollInCount)
      Vue.set(state.table.agentMonitor[event._id], 'sessionRollOutCount', event.sessionRollOutCount)
      Vue.set(state.table.agentMonitor[event._id], 'sessionCompleteCount', event.sessionCompleteCount)
      Vue.set(state.table.agentMonitor[event._id], 'agentInviteCount', event.agentInviteCount)
      Vue.set(state.table.agentMonitor[event._id], 'claimTime', event.claimTime)
      Vue.set(state.table.agentMonitor[event._id], 'pcState', event.pcState)
      Vue.set(state.table.agentMonitor[event._id], 'mobileState', event.mobileState)
      Vue.set(state.table.agentMonitor[event._id], 'pcAutoClaim', event.pcAutoClaim)
      Vue.set(state.table.agentMonitor[event._id], 'pcToggleNew', event.pcToggleNew)
      Vue.set(state.table.agentMonitor[event._id], 'mobileAutoClaim', event.mobileAutoClaim)
      Vue.set(state.table.agentMonitor[event._id], 'mobileClaimTime', event.mobileClaimTime)
      Vue.set(state.table.agentMonitor[event._id], 'pcClaimTime', event.pcClaimTime)
      Vue.set(state.table.agentMonitor[event._id], 'pcClaimState', event.pcClaimState)
      Vue.set(state.table.agentMonitor[event._id], 'pcCloseClaimTime', event.pcCloseClaimTime)
      // Vue.set(state.table.agentMonitor[event._id], 'mobileCloseClaimTime', event.mobileCloseClaimTime)
      Vue.set(state.table.agentMonitor[event._id], 'imDealCount', event.imDealCount)
      Vue.set(state.table.agentMonitor[event._id], 'agentState', peersState(state.table.agentMonitor[event._id]))
      peersStateChange(state, state.table.agentMonitor[event._id], 'plus')
      for (let key in state.table.MultiChannelSkill) {
        let item = state.table.MultiChannelSkill[key].onlineAgents
        if (item) {
          for (let i = 0; i < item.length; i++) {
            if (item[i].id === event._id) {
              Vue.set(state.table.MultiChannelSkill[key].onlineAgents[i], 'autoClaim', event.mobileAutoClaim || event.pcAutoClaim)
              break
            }
          }
        }
      }
    }

    Vue.set(state, 'agentStateChangeIm', {random: Math.random(), userId: event._id})
  },

  [types.EVENT_MONITOR_IM_QUEUE] (state, {event, curQueue}) {
    if (!state.table.MultiChannelSkill[event.queueExten]) {
      if (curQueue) {
        Vue.set(state.table.MultiChannelSkill, curQueue.Exten, curQueue)
      }
    }
    if (state.table.MultiChannelSkill[event.queueExten]) {
      Vue.set(state.table.MultiChannelSkill[event.queueExten], 'manualSession', event.manualSession)
      Vue.set(state.table.MultiChannelSkill[event.queueExten], 'waitClaim', event.waitClaim)
      // undealSession 未接入会话数
      // messageSession 留言数
      Vue.set(state.table.MultiChannelSkill[event.queueExten], 'undealSession', event.undealSession || 0)
      Vue.set(state.table.MultiChannelSkill[event.queueExten], 'messageSession', event.messageSession || 0)
      Vue.set(state.table.MultiChannelSkill[event.queueExten], 'queueGiveupCount', event.queueGiveUpCount || 0)
    }
    skillSummary(state)
  },
  [types.EVENT_MONITOR_MAIL] (state, {event}) {
    let agent
    if (!state.table.agentMonitor[event._id]) {
      agent = {
        _id: event._id,
        claim: event.claim,
        dealing: event.dealing,
        mailAutoClaim: event.mailAutoClaim // 邮件开关
      }
      Vue.set(state.table.agentMonitor, event._id, agent)
    } else {
      Vue.set(state.table.agentMonitor[event._id], 'mailAutoClaim', event.mailAutoClaim)
    }
    Vue.set(state, 'agentStateChangeIm', {random: Math.random(), userId: event._id})
  },
  [types.TABLEHEADER_AGENT] (state, {data}) {
    data.Config.splice(0, 4)
    Vue.set(state.tableHeader, 'agent', data)
  },
  [types.UPDATE_CLAIM_STATUS] (state, {user, status}) {
    if (state.table.agentMonitor && state.table.agentMonitor[user]) {
      Vue.set(state.table.agentMonitor[user], 'autoClaim', status)
    }
  },
  [types.AGENT_STATE_CHANGE] (state, userId) {
    // 用于非监控模块 如工作台
    if (userId !== 'delete' && userId !== 'deleteTemp' && userId !== 'deleteNew') {
      state.agentStateChange.randomBar = Math.random()
    }
    // 解决同时推过来多个坐席状态改变，只记录到最后一个坐席的bug
    if (userId !== 'delete' && userId !== 'deleteTemp' && userId !== 'deleteNew') {
      if (state.moduleOpen.call) {
        if (state.agentStateChange.userId.indexOf(userId) === -1) {
          state.agentStateChange.userId.push(userId)
        }
        if (state.agentStateChange.userIdtemp.indexOf(userId) === -1) {
          state.agentStateChange.userIdtemp.push(userId)
        }
      }
      if (state.moduleOpen.newCall && state.agentStateChange.userIdNew.indexOf(userId) === -1) {
        state.agentStateChange.userIdNew.push(userId)
      }
      Vue.set(state.agentStateChange, 'random', Math.random())
    } else if (userId === 'deleteTemp') {
      Vue.set(state.agentStateChange, 'userIdtemp', [])
    } else if (userId === 'delete') {
      Vue.set(state.agentStateChange, 'userId', [])
    } else if (userId === 'deleteNew') {
      Vue.set(state.agentStateChange, 'userIdNew', [])
    }
  },
  [types.MONITOR_MODULE_OPEN] (state, data) {
    Vue.set(state.moduleOpen, data.type, data.status)
  },
  [types.MULTI_SKILL_CHECK_INIT] (state, {key, onlineAgents}) {
    Vue.set(state.table.MultiChannelSkill[key], 'onlineAgents', onlineAgents)
  },
  [types.MULTI_SKILL_AGENT_CHECK] (state, {data, operate}) {
    let imQueues = state.table.MultiChannelSkill
    if (operate === 'add') {
      for (let key in imQueues) {
        let item = imQueues[key]
        for (let i = 0; i < item.AssignMembers.length; i++) {
          if (item.AssignMembers[i].agentID === data.userId) {
            let allAgents = state.table.agentMonitor
            var agentInfo = allAgents[data.userId] || ''
            if (!agentInfo) {
              agentInfo = {displayName: ''}
            }
            if (!agentInfo.displayName) {
              agentInfo.displayName = ''
            }
            var tmpAgent = {id: data.userId, name: agentInfo.displayName, autoClaim: agentInfo.autoClaim}
            if (item.onlineAgents) {
              let isOnQueue = item.onlineAgents.some((one, index) => {
                if (one.id === tmpAgent.id) {
                  Vue.set(item.onlineAgents, index, tmpAgent)
                  return true
                }
              })
              if (!isOnQueue) {
                // 去重，刷新时后端只推登陆，没推退出坐席
                item.onlineAgents.push(tmpAgent)
              }
              Vue.set(state.table.MultiChannelSkill[key], 'onlineAgents', item.onlineAgents)
            }
            break
          }
        }
      }
    } else {
      for (let key in imQueues) {
        let item = imQueues[key]
        let delIndex = -1
        let onlineAgents = item.onlineAgents || []
        for (let i = 0; i < onlineAgents.length; i++) {
          if (onlineAgents[i].id === data.userId) {
            delIndex = i
            break
          }
        }
        if (delIndex !== -1) {
          onlineAgents.splice(delIndex, 1)
          Vue.set(state.table.MultiChannelSkill[key], 'onlineAgents', onlineAgents)
        }
      }
    }
  }
}
function accessSummary (state) {
  let channel = state.table.MultiChannel
  let allSession = 0
  let closeSession = 0
  let accessSession = 0 // 人工处理会话
  let waitClaim = 0
  let messageSession = 0
  for (let i in channel) {
    let item = channel[i]
    messageSession += item.messageSession
    waitClaim += item.waitClaim
    allSession += item.newSession
    closeSession += item.closeSession
    accessSession += item.accessSession
  }
  state.webchatSummary.messageSession = messageSession
  state.webchatSummary.waitClaim = waitClaim
  state.webchatSummary.allSession = allSession
  state.webchatSummary.closeSession = closeSession
  state.webchatSummary.accessSession = accessSession
}
function peersState (item) {
  let state = ''
  if (isType(item, 'online')) {
    state = 'online'
    if (isType(item, 'close')) {
      state = 'close'
    } else if (isType(item, 'customize')) {
      state = 'customize'
    } else {
      if (isType(item, 'free')) {
        state = 'free'
      } else if (isType(item, 'busy')) {
        state = 'busy'
      }
    }
  } else {
    state = 'offline'
  }
  return state
}
function peersStateChange (state, item, type) {
  let current = item.agentState
  if (type === 'plus') {
    if (current) {
      state.webchatAgentStatus[current] ++
    }
    // 在线坐席且领取开关开着
    if (item.imDealCount && (item.mobileAutoClaim || item.pcAutoClaim)) {
      let num = parseInt(item.imDealCount - item.dealing)
      num = num > 0 ? num : 0
      state.webchatSummary.imDealCount += num
    }
    state.webchatSummary.dealingSession += (item.dealing || 0)
  } else if (type === 'minus') {
    if (current) {
      state.webchatAgentStatus[current] --
    }
    if (item.imDealCount && (item.mobileAutoClaim || item.pcAutoClaim)) {
      let num = parseInt(item.imDealCount - item.dealing)
      num = num > 0 ? num : 0
      state.webchatSummary.imDealCount -= num
    }
    state.webchatSummary.dealingSession -= (item.dealing || 0)
  }
}
function skillSummary (state) {
  let channel = state.table.MultiChannelSkill
  let undealSession = 0
  let queueGiveupCount = 0
  for (let i in channel) {
    let item = channel[i]
    undealSession += item.undealSession
    queueGiveupCount += item.queueGiveupCount
  }
  state.webchatSummary.undealSession = undealSession
  state.webchatSummary.queueGiveupCount = queueGiveupCount
}
// 初始化技能组在线坐席
export function imMonitorInitQueueAgentsNew (queue, onlines, state) {
  // var onlines = store.dispatch('getCache', {type: 'onlineChannelAgent', loadFromServer: true})
  let resultList = []
  let members = queue.AssignMembers || []
  for (var i = 0; i < members.length; i++) {
    var curId = members[i].agentID
    for (var j = 0; j < onlines.length; j++) {
      var agent = onlines[j]
      if (agent._id === curId) {
        var agentInfo = state.table.agentMonitor[curId]
        if (!agentInfo) {
          agentInfo = {displayName: ''}
        }
        if (!agentInfo.displayName) {
          agentInfo.displayName = ''
        }
        var tmpAgent = {id: curId, name: agentInfo.displayName, autoClaim: agentInfo.autoClaim}
        resultList.push(tmpAgent)
      }
    }
  }
  Vue.set(queue, 'onlineAgents', resultList)
}
